[

define date->timestamp::integer => .asInteger

// date->leapyear
define date->leapyear()::boolean => {
	local(year) = .year
	#year % 4   != 0 ? return(false)
	#year % 100 != 0 ? return(true)
	#year % 400 != 0 ? return(false)
	return(true)
}

/*
date->leapyear
date('1/1/2008')->leapyear
date('1/1/2000')->leapyear
date('1/1/1900')->leapyear
*/


define date->lastDayOfMonth::integer => {
	select(.month)
		case(1)
			return 31
		case(2)
			.leapyear ? return 29 | return 28
		case(3)
			return 31
		case(4)
			return 30
		case(5)
			return 31
		case(6)
			return 30
		case(7)
			return 31
		case(8)
			return 31
		case(9)
			return 30
		case(10)
			return 31
		case(11)
			return 30
		case(12)
			return 31
		case
			return // error
	/select
}

//date('2/1/2008')->lastdayofmonth


define date->startOfWeek(firstDayOfWeek::integer=.firstDayofWeek) => {
  (local(back) := .dayOfWeek - #firstDayOfWeek) < 0 ? #back += 7
  return .asCopy->subtract(-day=#back)&;
}
/*
date; ' -- '; 
date->startofweek(4); // wednesday
'<br>';

var(d) = date;
$d->firstDayOfWeek = 2; // monday
$d; ' -- '; $d->startOfWeek;
*/
]